<?php

namespace app\common\exceptions;

use Throwable;
use think\Response;
use think\facade\Log;
use think\exception\Handle;

class ExceptionHandle extends Handle
{
    /**
     * 不需要记录信息（日志）的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        ThrowException::class
    ];

    /**
     * 记录异常信息（包括日志或者其它方式记录）
     *
     * @access public
     * @param Throwable $exception
     * @return void
     */
    public function report(Throwable $exception): void
    {
        if (env('APP_DEBUG') == false && !($exception instanceof ThrowException)) {
            $log = $exception->getFile() . '--' . $exception->getLine() . '--' . $exception->getMessage();
            Log::write('error-ExceptionHandle:' . $log);
        }
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     * @access public
     * @param \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        return _fail($e->getCode(), $e->getMessage());
    }

}
